package com.bangbang.controller;


import com.bangbang.configresult.Result;
import com.bangbang.configresult.ResultEnum;
import com.bangbang.dto.UserDTO;
import com.bangbang.pojo.UserTest;
import com.bangbang.service.TestUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("test")
@Api(value = "测试用户服务接口")
public class TestController {


    @Autowired
    private TestUserService testUserService;

//  DeleteMapping 删除方法    PostMapping 作用新增,复杂业务等方法   PutMapping 作用在修改方法   GetMapping 作用在查询方法

    //            - - - - - - - - - - -  新增方法 - - - - - - - - - - - - -

    // 增
    @ApiOperation(value = "用户增加方法") //  接口文档方法名
    @ApiImplicitParams({            //  接口文档  解释参数
            @ApiImplicitParam(name = "TestUser",value = "用户的实体类",required = true,paramType = "body"),
            @ApiImplicitParam(name = "str",value = "其他参数",required = true,paramType = "query")
    })
    @PostMapping         //  post 请求 新增 复杂查询等方法
    public Result save(@RequestBody UserTest user, @RequestParam String str){
        System.out.println(str);
        testUserService.save(user);
        return Result.buildResult(ResultEnum.SUCCESS);
    }
//            - - - - - - - - - - -  删除方法 - - - - - - - - - - - - -
        //  删
        @ApiOperation(value = "用户删除方法")
        @ApiImplicitParams({
                @ApiImplicitParam(name = "TestUserId",value = "用户的id",required = true,paramType = "path"),
        })
        @DeleteMapping("/{TestUserId}")
        public Result delete(@PathVariable("TestUserId") Integer TestUserId){
            testUserService.delete(TestUserId);
            return Result.buildResult(ResultEnum.SUCCESS);
        }
//            - - - - - - - - - - -  修改方法 - - - - - - - - - - - - -

        // 改
        @ApiOperation(value = "用户修改方法")
        @ApiImplicitParams({
                @ApiImplicitParam(name = "UserTest",value = "用户的实体类",required = true,paramType = "body"),
        })
        @PutMapping
        public Result update(UserTest user){
            testUserService.update(user);
            return Result.buildResult(ResultEnum.SUCCESS);
        }

//            - - - - - - - - - - -  查新方法 - - - - - - - - - - - - -

    //  查
    @ApiOperation(value = "用户查询方法")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "TestUserId",value = "用户的id",required = true,paramType = "path"),
    })
    @GetMapping("/{TestUserId}")
    public Result selectById(@PathVariable("TestUserId") Integer TestUserId){
        UserTest user =  testUserService.selectById(TestUserId);

        UserDTO userDTO = new UserDTO();
        BeanUtils.copyProperties(user,userDTO);
        return Result.buildResult(ResultEnum.SUCCESS).setResult(userDTO);
    }


}
